/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinni.webservicestest.dbaccess;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author chandrakant
 */
public class WebServicesDbHelper {

    Session session = null;

    public WebServicesDbHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
//from User as user where user.uname='dsc110@gmail.com' works
    public User getUserByUserName(String uName) {

        User user = null;

        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from User as user where user.uname='" + uName+"'");
            user = (User) q.uniqueResult();
        } catch (Exception e) {
            System.err.println(e.toString());
            //add trace in address field for convenience
            user = new User("User", "", e.toString());
        }

        return user;
    }

    public List getUsers() {
        List<User> userList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from User as user");
            userList = (List<User>) q.list();
        } catch (Exception e) {
             System.err.println(e.toString());
             userList=new ArrayList<User>();
             userList.add(new User("User", "", e.getMessage()));             
        }
        return userList;
    }
}
